package com.woniuxy.project.campus.canteen.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.project.campus.canteen.mapper.AccountAddressMapper;
import com.woniuxy.project.campus.canteen.model.AccountAddress;
import com.woniuxy.project.campus.canteen.param.AddAddressParam;
import com.woniuxy.project.campus.canteen.param.UpdateAddressParam;
import com.woniuxy.project.campus.canteen.service.AccountAddressService;
import com.woniuxy.project.campus.canteenModelform.AddAddressForm;
import com.woniuxy.project.campus.canteenModelform.UpdateAddressForm;
import com.woniuxy.project.campus.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 猫将军
 * @since 2025年02月17日
 */
@RestController
@RequestMapping("/accountAddress")
public class AccountAddressController {

    @Resource
    private AccountAddressService accountAddressService;
    @Resource
    private AccountAddressMapper accountAddressMapper;

    //用户添加收货地址
    @ApiOperation(value = "用户添加收货地址")
    @PostMapping("/addAddress")
    public Result addAddress(@RequestHeader("token") String token,@RequestBody AddAddressForm form ){
        AddAddressParam bean = BeanUtil.toBean(form, AddAddressParam.class);
        bean.setToken(token);
        accountAddressService.addAddress(bean);
        return Result.success("添加成功");
    }


    @ApiOperation(value = "用户查询收货地址")
    @GetMapping("/getAddress")
    public Result getAddress(@RequestHeader("token") String token){
       List<AccountAddress> list = accountAddressService.getAddress(token);
        return Result.success(list);
    }

    //用户修改收货地址信息
@ApiOperation(value = "用户修改收货地址信息")
    @PostMapping("/updateAddress")
    public Result updateAddress(@RequestHeader("token") String token,@RequestBody UpdateAddressForm form){
        UpdateAddressParam bean = BeanUtil.toBean(form, UpdateAddressParam.class);
        bean.setToken(token);
        accountAddressService.deleteAddress(bean);
        return Result.success("修改成功");
    }

    //用户删除收货地址
    @ApiOperation(value = "用户删除收货地址")
    @DeleteMapping("/deleteAddress")
    public Result deleteAddress(@RequestHeader("token") String token,Integer addressId){
        accountAddressService.deleteAddress(token,addressId);
        return Result.success("删除成功");
    }
    //用户设置地址为默认地址
    @ApiOperation(value = "用户设置地址为默认地址")
    @PostMapping("/setDefaultAddress")
    public Result setDefaultAddress(@RequestHeader("token") String token,Integer addressId){
        accountAddressService.setDefaultAddress(token,addressId);
        return Result.success("设置成功");
    }


    //通过收货信息 查询id
    @ApiOperation(value = "通过收货信息 查询id")
    @PostMapping("/getAddressId")
    public Result getAddressId(@RequestBody AddAddressForm form){
        Integer id = accountAddressService.getAddressId(form);
        return Result.success(id);
    }

    //通过id查询收货地址信息
    @ApiOperation(value = "通过id查询收货地址信息")
    @GetMapping("/getAddressByAddressId")
    public Result getAddressByAddressId(@RequestParam("id")Integer id){
        QueryWrapper<AccountAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        AccountAddress accountAddress = accountAddressMapper.selectOne(wrapper);
        wrapper.clear();
        return Result.success(accountAddress);
    }


}

